﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace SortBuildOutput
{
    public partial class fmMainForm : Form
    {
        public fmMainForm()
        {
            InitializeComponent();
        }

        private void btSort_Click(object sender, EventArgs e)
        {
            var orderedOutput = new Dictionary<int, StringBuilder>();
            foreach (var line in tbText.Lines)
            {
                int process = 0;
                var re = new Regex(@"^(?<process>\d+)\>.*$");
                if (re.IsMatch(line))
                {
                    var match = re.Match(line);
                    process = Convert.ToInt32(match.Groups["process"].Value);
                }

                if (!orderedOutput.ContainsKey(process))
                {
                    orderedOutput[process] = new StringBuilder();
                }
                orderedOutput[process].AppendLine(line);
            }

            tbText.Clear();

            foreach (var i in orderedOutput.Keys)
            {
                tbText.AppendText(orderedOutput[i].ToString());
            }

            tbText.SelectAll();
            tbText.Focus();
        }
    }
}
